<template>
    <div class="index_root_view">
        <!-- <img class="bg_img" :focusable="false" src="../assets/home/bg.png"> -->
        <!-- <span>登录页</span> -->
    </div>
</template>


<script lang="ts">
import { ref } from "vue";
import { defineComponent } from '@vue/runtime-core'
import { useESRouter } from "@extscreen/es3-router"
import { useGlobalApi } from "../api/UseApi";
import BuildConfig from '../build/BuildConfig'
import { useESDevice, useESToast, useESLocalStorage } from '@extscreen/es3-core'
import { createESContentProviderModule } from '@extscreen/es3-content-provider';
import { probe } from '../utils/probe'

export default defineComponent({
    name: 'index',
    setup() {
        const router = useESRouter()
        const toast = useESToast()
        const localStorage = useESLocalStorage()
        const globalApi = useGlobalApi()
        const device = useESDevice()
        const provider = createESContentProviderModule()
        const authInfo = ref()

        const onESCreate = (params) => {
            // todo  推荐位跳转

            const deviceInfo = getDeviceInfo()
            console.log('deviceInfo >>>', JSON.stringify(deviceInfo));
            if (deviceInfo.mac === '00dbd68e478e') {
                // 模拟器
                globalApi
                    .userRegister({
                        accountType: '0',
                        areaCode: '',
                        cid: BuildConfig.cid,
                        deviceName: deviceInfo.model,
                        passwd: 'string',
                        subAreaCode: '',
                        userAccount: deviceInfo.mac
                    })
                    .then(async (res) => {
                        if (res.code == 200) {
                            // console.log(res, '注册成功 >>>>');
                            // toast.showToast('注册成功 !')
                            await localStorage.putString('mac', deviceInfo.mac)
                            await localStorage.putString('deviceName', deviceInfo.model)
                            await localStorage.putString('userAccount', res.data.userAccount)
                            await localStorage.putString('userId', res.data.userId)
                            userLoginHook(res.data.userId)
                        }
                    })
            } else {
                // 机顶盒
                // {"brand":"CM311-5-CH","mac":"24d9049b576f","esMac":"24d9049b576f","esWifiMac":"","manufacturer":"CMDC","model":"CM311-5-CH","deviceVersion":"9"}
                getSTBAuthInfo()
            }
        }

        function getSTBAuthInfo() {
            console.log('BuildConfig.name_uri >>>', BuildConfig.name_uri);
            provider.query(BuildConfig.name_uri).then(
                value => {
                    console.log(JSON.stringify(value), 'name_uri >>>>>');
                    // {"result":1,"data":[{"name":"username","_id":"50","value":"24D9049B576F"}]} 
                    if (value.data) {
                        // toast.showToast('查询结果：' + value.data[0].value);
                        let username = value.data[0].value
                        userRegisterHook(username)
                    }
                    // authInfo.value = JSON.stringify(data);
                },
                error => { },
            );
            // provider.query(token_uri).then(
            //     value => {
            //         // {"result":1,"data":[{"name":"user_token","_id":"51","value":"5@Q6oAzvyqyRysfiRh2bRX0136729851"}]}
            //         if (value.data) {
            //             toast.showToast('查询结果：' + value.data[0].value);
            //         }
            //     },
            //     error => { },
            // );
            provider.query(BuildConfig.server_uri).then(
                async value => {
                    console.log(JSON.stringify(value), 'server_uri >>>>>');
                    // {"result":1,"data":[{"name":"epg_server","_id":"52","value":"http://111.20.105.223:9330"}]} 
                    if (value.data) {
                        let data = value.data[0]
                        // toast.showToast('查询结果：' + data.value);
                        await localStorage.putString('epg_server', data.value)
                    }
                },
                error => { },
            );
            // provider.query(BuildConfig.token_uri).then(
            //     async value => {
            //         console.log(JSON.stringify(value), 'token_uri >>>>>');
            //         if (value.data) {
            //             let data = value.data[0]
            //             // await localStorage.putString('epg_server', data.value)
            //         }
            //     },
            //     error => { },
            // );
        }

        // 注册 
        function userRegisterHook(username) {
            // username 从provide 
            const deviceInfo = getDeviceInfo()
            if (username) {
                globalApi
                    .userRegister({
                        accountType: '0',
                        areaCode: '',
                        cid: BuildConfig.cid,
                        deviceName: deviceInfo.model,
                        passwd: 'string',
                        subAreaCode: '',
                        userAccount: username
                    })
                    .then(async (res) => {
                        if (res.code == 200) {
                            // console.log(res, '注册成功 >>>>');
                            // toast.showToast('注册成功 !')
                            await localStorage.putString('deviceName', deviceInfo.model)
                            await localStorage.putString('userAccount', res.data.userAccount)
                            await localStorage.putString('userId', res.data.userId)
                            userLoginHook(res.data.userId)
                        }
                    })
            }
        }

        // 登录
        function userLoginHook(userId) {
            globalApi
                .userLogin({
                    cid: BuildConfig.cid,
                    clientVersion: 'optional',
                    userId: userId,
                    platform: 'NO',
                    fromPosition: 'NO'
                })
                .then(async (res: any) => {
                    // save token 
                    const token = res.data.tokenHead + ' ' + res.data.userToken
                    await localStorage.putString('user_token', token)
                    // toast.showToast('登录成功 !')
                    // jump to  home 
                    // router.push({
                    //     name: 'home',
                    //     params: {}
                    // })

                    // 测试
                   
                    // 埋点  启动
                    probe.init(()=> {
                        probe.startApp()
                    })

                    router.push({
                        name: 'home',
                        replace: true
                    })

                    // router.push({
                    //     name: 'ropeSkipping',
                    //     params: {
                    //         id: 62
                    //     },
                    //     replace: true
                    // })
                    // router.push({
                    //     name: 'sixButtonPlay',
                    //     params: {
                    //       id: 4
                    //     }
                    // })

                    // router.push({
                    //     name: 'scanPlay',
                    //     replace: true
                    // })
                         
                    // router.push({
                    //     name: 'fullPlay',
                    //              params: {
                    //             type: '0'    
                    //            },
                    //     replace: true
                    // })


                    // router.push({
                    //     name: 'finishDance',
                    //     replace: true
                    // })

                    // router.push({
                    //     name: 'sixButtonPlay',
                    //     params: {
                    //         id: 1
                    //     },
                    //     replace: true
                    // })

                    // router.push({
                    //     name: 'aiStamina',
                    //     params: {
                    //         id: 39
                    //     },
                    //     replace: true
                    // })

                    // router.push({
                    //     name: 'medal',
                    //     replace: true
                    // })

                    // router.push({
                    //     name: 'sportsReport',
                    //     // params: {
                    //     //     id: 1
                    //     // },
                    //     replace: true
                    // })
                })
        }

        // 获取设备信息
        function getDeviceInfo() {
            let mac = device.getDeviceEthMac()
            let es_mac = device.getDeviceEthMac()
            if (!mac) {
                mac = device.getDeviceWifiMac()
            }
            let wifi_mac = device.getDeviceWifiMac()
            mac = mac.replace(/:/g, '').toLowerCase()
            if (es_mac) {
                es_mac = es_mac.replace(/:/g, '').toLowerCase()
            }
            if (wifi_mac) {
                wifi_mac = wifi_mac.replace(/:/g, '').toLowerCase()
            }

            let res = {
                brand: device.getBuildBrand(),
                mac: mac,
                esMac: es_mac,
                esWifiMac: wifi_mac, // wifi mac
                manufacturer: device.getBuildManufacturer(), // 制造商
                model: device.getBuildModel(), // 设备型号
                deviceVersion: device.getBuildVersionRelease() // 版本
            }
            console.log(res, '设备信息 >>>>')
            // {
            //     brand: "CM311-5-CH",
            //     deviceVersion: "9",
            //     esMac: "24d9049b576f",
            //     esWifiMac: "",
            //     mac: "24d9049b576f",
            //     manufacturer: "CMDC",
            //     model: "CM311-5-CH",
            // }
            return res
        }

        return {
            onESCreate
        }
    }
})
</script>

<style scoped>
.index_root_view {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 1920px;
    height: 1080px;
    /* background-color: transparent; */
    background-color: #000;
}

.bg_img {
    width: 1920px;
    height: 1080px;
    position: absolute;
    top: 0;
    left: 0;
}

.index_root_view span {
    font-size: 50px;
    color: #fff;
}
</style>